﻿namespace PrismEx.SampleApp.Model
{
    using System.ComponentModel.Composition;
    using System.Data.Entity;

    [Export]
    public class DataContext : DbContext
    {
        static DataContext()
        {
            Database.SetInitializer<DataContext>(new DataContext.Initializer());
        }

        public DbSet<Customer> Customers { get; set; }

        private class Initializer : IDatabaseInitializer<DataContext>
        {
            public void InitializeDatabase(DataContext context)
            {
                if (!context.Database.Exists() || !context.Database.CompatibleWithModel(true))
                {
                    context.Database.Delete();
                    context.Database.Create();

                    CreateFakeData(context);
                }
            }
        }

        private static void CreateFakeData(DataContext context)
        {
            context.Customers.Add(new Customer() { FirstName = "Bob", LastName = "Smith", Email = "bob@bobsmith.com", Phone = "07977876657", Gender = "Male" });
            context.Customers.Add(new Customer() { FirstName = "David", LastName = "Jones", Email = "david@davidjones.com", Phone = "07977654567", Gender = "Male" });
            context.Customers.Add(new Customer() { FirstName = "Emilia", LastName = "Good", Email = "emigood@msn.com", Phone = "07989876765", Gender = "Female" });
            context.Customers.Add(new Customer() { FirstName = "Claire", LastName = "Stevenson", Email = "claire_stevenson_89@hotmail.com", Phone = "07965456765", Gender = "Female" });
            context.Customers.Add(new Customer() { FirstName = "Peter", LastName = "Jones", Email = "peterjones@gmail.com", Phone = "07909345897", Gender = "Male" });
            context.Customers.Add(new Customer() { FirstName = "Karl", LastName = "Campbell", Email = "kcambell@bttotalbroadband.com", Phone = "07991768123", Gender = "Male" });
            context.Customers.Add(new Customer() { FirstName = "Tiffany", LastName = "Sloan", Email = "tsloan@plus.net", Phone = "07876540924", Gender = "Female" });
            context.SaveChanges();
        }
    }
}